import { Space, Tag } from "antd";
import { DescriptionProps } from "./data";
import { forwardRef, useState } from "react";
import { ProDescriptions } from "@ant-design/pro-components";
import styles from './descriptionStyles.less';

const Description: React.FC<DescriptionProps> = (props, uploadRef) => {
  // const [props.checkedKeys, setprops.checkedKeys] = useState<React.Key[]>([]);
  const driveTypeMap = new Map([
    [1, 'V90'],
    [2, 'S210'],
  ]);

  const encoderMap = new Map([
    [0, '单圈绝对编码器'],
    [1, '多圈绝对编码器'],
    [2, '增量式编码器'],
  ]);

  const brakeMap = new Map([
    [0, '不带抱闸'],
    [1, '带抱闸'],
  ]);

  const loadTypeMap = new Map([
    [1, '水平'],
    [2, '旋转'],
    [3, '垂直'],
    [4, '斜坡'],
    [5, '其它'],
  ]);

  const mechanicalStructureMap = new Map([
    [1, '丝杠'],
    [2, '齿轮齿条'],
    [3, '皮带轮'],
    [4, '圆盘'],
    [5, '辊式带'],
    [6, '链条'],
    [7, '其它'],
  ]);

  const controlModeMap = new Map([
    [1, 'TO'],
    [2, 'Epos'],
    [3, 'Speed'],
  ]);

  const commTelegramMap = new Map([
    [105, '105'],
    [111, '111'],
    [102, '102'],
    [3, '3'],
    [1, '1'],
  ]);

  const additionalTelegramMap = new Map([
    [999, 'None'],
    [750, '750'],
  ]);

  const motorRotationMap = new Map([
    [0, '正向'],
    [1, '反向'],
  ]);

  const originalMap = new Map([
    [0, '来自设备管理'],
    [1, '其他'],
  ]);

  const diMap = new Map([
    [0, 'NA'],
    [2, 'RESET'],
    [3, 'CWL'],
    [4, 'CCWL'],
    [11, 'TLIM'],
    [20, 'SLIM'],
    [24, 'REF'],
    [29, 'EMGS'],
  ]);

  const doMap = new Map([
    [1, 'RDY'],
    [2, 'FAULT'],
    [3, 'INP'],
    [4, 'ZSP'],
    [6, 'TLR'],
    [8, 'MBR'],
    [9, 'OLL'],
    [12, 'REFOK'],
    [14, 'RDY_ON'],
    [15, 'STO'],
    [16, 'PZD1'],
    [17, 'PZD2'],
  ]);

  const modualActiveMap = new Map([
    [1, 'Yes'],
    [0, 'No'],
  ]);

  const pzdMap = new Map([
    [0, '无功能'],
    [1, '实际扭矩'],
    [2, '实际绝对电流'],
    [3, 'DI状态'],
  ]);

  return (
    <div>
      <ProDescriptions
        title={<div style={{ fontSize: '14px' }}>基础信息配置</div>}
        style={{ marginTop: '22px' }}
        contentStyle={{ lineHeight: 0.4 }}
        labelStyle={{ lineHeight: 0.4, color: 'grey' }}
        column={3}
        request={async() => {
          return Promise.resolve({
            success: true,
            data: {
              shaftName: props.row.shaftName,
              driveType: driveTypeMap.get(props.row.driveType),
              motorOrderNum: props.row.motorOrderNum,
              hardwareIdentifier: props.row.hardwareIdentifier,
            },
          });
        }}
        columns={[
          {
            title: "轴名称",
            key: 'shaftName',
            dataIndex: 'shaftName',
          },
          {
            title: "驱动类型",
            key: 'driveType',
            dataIndex: 'driveType',
          },
          {
            title: "硬件标识符",
            key: 'hardwareIdentifier',
            dataIndex: 'hardwareIdentifier',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "电机订货号",
            key: 'motorOrderNum',
            dataIndex: 'motorOrderNum',
          },
        ]}
      ></ProDescriptions>

      <ProDescriptions
        title={<div style={{ fontSize: '14px' }}>电机配置数据</div>}
        column={3}
        contentStyle={{ lineHeight: 0.4 }}
        labelStyle={{ lineHeight: 0.4, color: 'grey' }}
        request={async() => {
          return Promise.resolve({
            success: true,
            data: {
              motorId: props.row.motorId,
              ecoderType: encoderMap.get(props.row.ecoderType),
              brake: brakeMap.get(props.row.brake),
              tbrakeOpen: props.row.tbrakeOpen,
              tbrakeClose: props.row.tbrakeClose,
              ratedVoltage: props.row.ratedVoltage,
              ratedCurrent: props.row.ratedCurrent,
              ratedPower: props.row.ratedPower,
              ratedSpeed: props.row.ratedSpeed,
              ratedTorque: props.row.ratedTorque,
              motorInertia: props.row.motorInertia,
            },
          });
        }}
        columns={[
          {
            title: "电机ID",
            key: 'motorId',
            dataIndex: 'motorId',
          },
          {
            title: "编码器类型",
            key: 'ecoderType',
            dataIndex: 'ecoderType',
          },
          {
            title: "抱闸配置 P1215",
            key: 'brake',
            dataIndex: 'brake',
          },
          {
            title: "抱闸打开时间 P1216 [ms]",
            key: 'tbrakeOpen',
            dataIndex: 'tbrakeOpen',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "抱闸闭合时间 P1217 [ms]",
            key: 'tbrakeClose',
            dataIndex: 'tbrakeClose',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "额定电压 P304 [V]",
            key: 'ratedVoltage',
            dataIndex: 'ratedVoltage',
          },
          {
            title: "额定电流 P305 [A]",
            key: 'ratedCurrent',
            dataIndex: 'ratedCurrent',
          },
          {
            title: "额定功率 P307 [kW]",
            key: 'ratedPower',
            dataIndex: 'ratedPower',
          },
          {
            title: "额定速度 P311 [rpm]",
            key: 'ratedSpeed',
            dataIndex: 'ratedSpeed',
          },
          {
            title: "额定转矩 P312 [Nm]",
            key: 'ratedTorque',
            dataIndex: 'ratedTorque',
          },
          {
            title: "电机转动惯量 P341 [kg.m2]",
            key: 'motorInertia',
            dataIndex: 'motorInertia',
          },
        ]}
      ></ProDescriptions>

      <div hidden={props.isEngineer || props.isCalc}>
        <ProDescriptions
          title={<div style={{ fontSize: '14px' }}>默认参数</div>}
          column={2}
          contentStyle={{ lineHeight: 0.4 }}
          labelStyle={{ lineHeight: 0.4, color: 'grey' }}
            request={async() => {
            return Promise.resolve({
              success: true,
              data: {
                kpDefault: props.row.kpDefault,
                tnDefault: props.row.tnDefault,
                kprDefault: props.row.kprDefault,
                posFilterDefault: props.row.posFilterDefault,
              },
            });
          }}
          columns={[
            {
              title: "速度环增益默认Kp P29120 [Nms/rad]",
              key: 'kpDefault',
              dataIndex: 'kpDefault',
            },
            {
              title: "速度环积分默认Tn P29121 [ms]",
              key: 'tnDefault',
              dataIndex: 'tnDefault',
            },
            {
              title: "位置环增益默认 P29110 [1000/min]",
              key: 'kprDefault',
              dataIndex: 'kprDefault',
            },
            {
              title: "位置环滤波默认 P2533 [1000/min]",
              key: 'posFilterDefault',
              dataIndex: 'posFilterDefault',
            },
          ]}
        ></ProDescriptions>
      </div>

      <ProDescriptions
        title={<div style={{ fontSize: '14px' }}>驱动配置</div>}
        contentStyle={{ lineHeight: 0.4 }}
        labelStyle={{ lineHeight: 0.4, color: 'grey' }}
        request={async() => {
          return Promise.resolve({
            success: true,
            data: {
              loadType: loadTypeMap.get(props.row.loadType),
              mechanicalStructure: mechanicalStructureMap.get(props.row.mechanicalStructure),
              commTelegram: commTelegramMap.get(props.row.commTelegram),
              additionalTelegram: additionalTelegramMap.get(props.row.additionalTelegram),
              controlMode: controlModeMap.get(props.row.controlMode),
              motorRotation: motorRotationMap.get(props.row.motorRotation),
              velocityMax: props.row.velocityMax,
              refSpeed: props.row.refSpeed,
              refCurrent: props.row.refCurrent,
              refTorque: props.row.refTorque,
              di1Epos: diMap.get(props.row.di1Epos),
              di2Epos: diMap.get(props.row.di2Epos),
              di3Epos: diMap.get(props.row.di3Epos),
              di4Epos: diMap.get(props.row.di4Epos),
              do1Epos: doMap.get(props.row.do1Epos),
              do2Epos: doMap.get(props.row.do2Epos),
              torqueLimUpper: props.row.torqueLimUpper,
              torqueLimLower: props.row.torqueLimLower,
            },
          });
        }}
        columns={[
          {
            title: "负载类型",
            key: 'loadType',
            dataIndex: 'loadType',
          },
          {
            title: "机械结构",
            key: 'mechanicalStructure',
            dataIndex: 'mechanicalStructure',
          },
          {
            title: "控制报文 P922",
            key: 'commTelegram',
            dataIndex: 'commTelegram',
          },
          {
            title: "附加报文 P8864",
            key: 'additionalTelegram',
            dataIndex: 'additionalTelegram',
          },
          {
            title: "控制模式",
            key: 'controlMode',
            dataIndex: 'controlMode',
          },
          {
            title: "电机旋转方向",
            key: 'motorRotation',
            dataIndex: 'motorRotation',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "电机最大转速 P1082 [rpm]",
            key: 'velocityMax',
            dataIndex: 'velocityMax',
          },
          {
            title: "参考转速 P2000 [rpm]",
            key: 'refSpeed',
            dataIndex: 'refSpeed',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "参考电流 P2002 [Arms]",
            key: 'refCurrent',
            dataIndex: 'refCurrent',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "参考转矩 P2003 [Nm]",
            key: 'refTorque',
            dataIndex: 'refTorque',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "DI1 P29301",
            key: 'di1Epos',
            dataIndex: 'di1Epos',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "DI2 P29302",
            key: 'di2Epos',
            dataIndex: 'di2Epos',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "DI3 P29303",
            key: 'di3Epos',
            dataIndex: 'di3Epos',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "DI4 P29304",
            key: 'di4Epos',
            dataIndex: 'di4Epos',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "DO1 P29330",
            key: 'do1Epos',
            dataIndex: 'do1Epos',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "DO2 P29331",
            key: 'do2Epos',
            dataIndex: 'do2Epos',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "转矩限幅上限 P29050[0] [%]",
            key: 'torqueLimUpper',
            dataIndex: 'torqueLimUpper',
            hideInDescriptions: !props.isCalc,
          },
          {
            title: "转矩限幅下限 P29051[0] [%]",
            key: 'torqueLimLower',
            dataIndex: 'torqueLimLower',
            hideInDescriptions: !props.isCalc,
          },
        ]}
      ></ProDescriptions>

      {/* <div style={{
        fontWeight: 'bold',
        fontSize: '14px',
        lineHeight: 1.5715,
        whiteSpace: "nowrap",
        color: 'black',
        flex: "auto",
        textOverflow: "ellipsis",
        overflow: "hidden",
      }}>
        工艺参数
      </div> */}
      <div>
        <ProDescriptions
          title={<div style={{ fontSize: '14px' }}>工艺参数</div>}
          contentStyle={{ lineHeight: 0.4 }}
          labelStyle={{ lineHeight: 0.4, color: 'grey' }}
          style={{ marginTop: '10px' }}
          column={2}
            request={async () => {
            return Promise.resolve({
              success: true,
              data: {
                dynamicCoeff: props.row.dynamicCoeff,
              },
            });
          }}
          columns={[
            {
              title: "动态系数",
              key: 'dynamicCoeff',
              dataIndex: 'dynamicCoeff',
              hideInDescriptions: !props.isCalc,
            },
          ]}
        ></ProDescriptions>
      </div>    
      <div hidden={props.row.controlMode != 2}>
        <ProDescriptions
          title={<div style={{ fontSize: '14px', color: "rgba(0, 0, 0, 0.65)" }}>EPOS</div>}
          // style={{ marginTop: '10px' }}
          column={2}
          contentStyle={{ lineHeight: 0.4 }}
          labelStyle={{ lineHeight: 0.4, color: 'grey' }}
            request={async () => {
            return Promise.resolve({
              success: true,
              data: {
                gearRationLoad: props.row.gearRationLoad,
                gearRationMotor: props.row.gearRationMotor,
                positionAccuracy: props.row.positionAccuracy,
                luLoopEpos: props.row.luLoopEpos,
                kprEpos: props.row.kprEpos,
                posFilterEpos: props.row.posFilterEpos,
                coeffVelEpos: props.row.coeffVelEpos,
                vmaxEpos: 6 * props.row.vmaxEpos * props.row.positionAccuracy,
                accmaxEpos: props.row.accmaxEpos,
                dccmaxEpos: props.row.dccmaxEpos,
                jerkEpos: props.row.jerkEpos,
                jerkActiveEpos: props.row.jerkActiveEpos,
                torquePreCtrl: props.row.torquePreCtrl,
                refApproachVel: props.row.refApproachVel,
                refSearchVel: props.row.refSearchVel,
                modualActiveEpos: modualActiveMap.get(props.row.modualActiveEpos),
                modualLengthEpos: props.row.modualLengthEpos,
                jog1VelEpos: props.row.jog1VelEpos,
                jog2VelEpos: props.row.jog2VelEpos,
                softLimitPositive: props.row.softLimitPositive,
                softLimitNegative: props.row.softLimitNegative,
                refCoordinate: props.row.refCoordinate,
                pzdSend: pzdMap.get(props.row.pzdSend),
              },
            });
          }}
          columns={[
            {
              title: "负载转速 (EPOS) P29248 [rpm]",
              key: 'gearRationLoad',
              dataIndex: 'gearRationLoad',
            },
            {
              title: "电机转速 (EPOS) P29249 [rpm]",
              key: 'gearRationMotor',
              dataIndex: 'gearRationMotor',
            },
            {
              title: "10mm对应的LU (EPOS)",
              key: 'positionAccuracy',
              dataIndex: 'positionAccuracy',
            },
            {
              title: "每转LU (EPOS) P29247",
              key: 'luLoopEpos',
              dataIndex: 'luLoopEpos',
            },
            {
              title: "位置环增益 (EPOS) P29110 [1000/min]",
              key: 'kprEpos',
              dataIndex: 'kprEpos',
            },
            {
              title: "位置环滤波 (EPOS) P2533 [1000/min]",
              key: 'posFilterEpos',
              dataIndex: 'posFilterEpos',
            },
            {
              title: "速度前馈系数 (EPOS) P29111 [%]",
              key: 'coeffVelEpos',
              dataIndex: 'coeffVelEpos',
            },
            {
              title: "EPOS 最大速度 P2571 [1000LU/min]",
              key: 'vmaxEpos',
              dataIndex: 'vmaxEpos',
            },
            {
              title: "EPOS 最大加速度 P2572 [1000 LU/s²]",
              key: 'accmaxEpos',
              dataIndex: 'accmaxEpos',
            },
            {
              title: "EPOS 最大减速度 P2573 [1000 LU/s²]",
              key: 'dccmaxEpos',
              dataIndex: 'dccmaxEpos',
            },
            {
              title: "EPOS 急动限制 P2574 [1000 LU/s³]",
              key: 'jerkEpos',
              dataIndex: 'jerkEpos',
            },
            {
              title: "EPOS 急动限制激活 P2575",
              key: 'jerkActiveEpos',
              dataIndex: 'jerkActiveEpos',
            },
            {
              title: "推力前馈LR质量/推力前馈质量 (EPOS) P2567",
              key: 'torquePreCtrl',
              dataIndex: 'torquePreCtrl',
              hideInDescriptions: !props.isCalc,
            },
            {
              title: "回零参考点逼近速度 (EPOS) P2605 [1000LU/min]",
              key: 'refApproachVel',
              dataIndex: 'refApproachVel',
              hideInDescriptions: !props.isCalc,
            },
            {
              title: "回零参考点搜索速度 (EPOS) P2608 [1000LU/min]",
              key: 'refSearchVel',
              dataIndex: 'refSearchVel',
              hideInDescriptions: !props.isCalc,
            },
            {
              title: "模态轴激活 (EPOS) P29245",
              key: 'modualActiveEpos',
              dataIndex: 'modualActiveEpos',
              hideInDescriptions: !props.isCalc,
            },
            {
              title: "模数补偿范围 (EPOS) P29246 [LU]",
              key: 'modualLengthEpos',
              dataIndex: 'modualLengthEpos',
              hideInDescriptions: !props.isCalc,
            },
            {
              title: "JOG1速度 (EPOS) P2585 [1000 LU/min]",
              key: 'jog1VelEpos',
              dataIndex: 'jog1VelEpos',
              hideInDescriptions: !props.isCalc,
            },
            {
              title: "JOG2速度 (EPOS) P2586 [1000 LU/min]",
              key: 'jog2VelEpos',
              dataIndex: 'jog2VelEpos',
              hideInDescriptions: !props.isCalc,
            },
            {
              title: "正软限位开关 (EPOS) p2580 [LU]",
              key: 'softLimitPositive',
              dataIndex: 'softLimitPositive',
              hideInDescriptions: !props.isCalc,
            },
            {
              title: "负软限位开关 (EPOS) p2581 [LU]",
              key: 'softLimitNegative',
              dataIndex: 'softLimitNegative',
              hideInDescriptions: !props.isCalc,
            },
            {
              title: "绝对编码器校正位置 (EPOS) P2599 [LU]",
              key: 'refCoordinate',
              dataIndex: 'refCoordinate',
              hideInDescriptions: !props.isCalc,
            },
            {
              title: "自定义PZD发送字 (EPOS) P29151",
              key: 'pzdSend',
              dataIndex: 'pzdSend',
              hideInDescriptions: !props.isCalc,
            },
          ]}
        ></ProDescriptions>
      </div>
      <div hidden={props.row.controlMode != 3}>
        <ProDescriptions
          title={<div style={{ fontSize: '14px', color: 'grey' }}>Speed</div>}
          // style={{ marginTop: '10px' }}
          column={2}
          contentStyle={{ lineHeight: 0.4 }}
          labelStyle={{ lineHeight: 0.4, color: 'grey' }}
            request={async () => {
            return Promise.resolve({
              success: true,
              data: {
                kpSpeed: props.row.kpSpeed,
                tnSpeed: props.row.tnSpeed,
                tincSpeed: props.row.tincSpeed,
                tdecSpeed: props.row.tdecSpeed,
                tstartSpeed: props.row.tstartSpeed,
                tstopSpeed: props.row.tstopSpeed,
                off3TdecSpeed: props.row.off3TdecSpeed,
                tmonitorSpeed: props.row.tmonitorSpeed,
              },
            });
          }}
          columns={[
            {
              title: "速度环增益Kp (Speed) V90 P29120 [Nms/rad] /S210 P1460",
              key: 'kpSpeed',
              dataIndex: 'kpSpeed',
            },
            {
              title: "速度环积分时间Tn (Speed) V90 P29121 [ms] / S210 P1462",
              key: 'tnSpeed',
              dataIndex: 'tnSpeed',
            },
            {
              title: "斜坡上升时间 (Speed) P1120 [s]",
              key: 'tincSpeed',
              dataIndex: 'tincSpeed',
            },
            {
              title: "斜坡下降时间 (Speed) P1121 [s]",
              key: 'tdecSpeed',
              dataIndex: 'tdecSpeed',
            },
            {
              title: "斜坡圆弧起始时间 (Speed) P1130 [s]",
              key: 'tstartSpeed',
              dataIndex: 'tstartSpeed',
            },
            {
              title: "斜坡圆弧结束时间 (Speed) P1131 [s]",
              key: 'tstopSpeed',
              dataIndex: 'tstopSpeed',
            },
            {
              title: "OFF3斜坡下降时间 (Speed) P1135 [s]",
              key: 'off3TdecSpeed',
              dataIndex: 'off3TdecSpeed',
              hideInDescriptions: !props.isCalc,
            },
            {
              title: "静态检测的监控时间 (Speed) P1227 [s]",
              key: 'tmonitorSpeed',
              dataIndex: 'tmonitorSpeed',
              hideInDescriptions: !props.isCalc,
            },
          ]}
        ></ProDescriptions>
      </div>    
    </div>
  );
};

const DriveParaDescription = forwardRef<unknown, DescriptionProps>(Description);

export default DriveParaDescription;